package com.qk.controller;

import com.qk.commen.PageResult;
import com.qk.commen.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    /**
     * 1.客户列表
     */
   @GetMapping
    public Result list(CustomerDto customerDto) {
       log.info("查询客户列表:{}", customerDto);
       PageResult<Customer> pageResult = customerService.listCustomers(customerDto);
        return Result.success(pageResult);
    }
    /**
     * 2.新增客户(add customer)
     */
    @PostMapping
    public Result add(@RequestBody Customer customer) {
        log.info("新增客户:{}", customer);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customer.setId(null);
        customerService.save(customer);
        return Result.success();
    }
    /**
     * 3.查询用户根据id
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询用户:{}", id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }
    /**
     * 4.修改客户信息
     */
    @PutMapping
    public Result update(@RequestBody Customer customer) {
        log.info("修改客户:{}", customer);
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success();
    }
}
